package com.zhongwang.business.module.baseCostManage.service;

import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.User;
import com.zhongwang.business.module.baseCostManage.dao.InputImportCostDao;
import com.zhongwang.business.module.baseCostManage.listener.InputExtProPlanListener;
import com.zhongwang.business.module.baseCostManage.listener.InputRolledSteelPlanListener;
import com.zhongwang.business.module.baseCostManage.model.DictCommonModel;
import com.zhongwang.business.module.baseCostManage.model.InputExtProPlanModel;
import com.zhongwang.business.util.FileUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 *  服务层
 * @author China.kxx
 * @date 2020/10/26 18:34
 */
@Service
public class InputExtProPlanService {
    @Autowired
    private InputImportCostDao inputImportCostDao;
    @Value("${user.upload.path}")
    private String uploadPath;
    private String machining = "";
    /**
     * 查询
     * @param inputExtProPlanModel
     * @return 外协计划列表数据
     */
    public List<InputExtProPlanModel> inputExtProPlan (InputExtProPlanModel inputExtProPlanModel) {

        List<InputExtProPlanModel> particulars = inputImportCostDao.selectInputExtProPlan(inputExtProPlanModel);
        return particulars;

    }

    /**
     * 导入外购、外协、原料计划文件
     * @param file 导入文件
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public String importFile(MultipartFile file, InputExtProPlanModel inputExtProPlanModel) {
        try {
            FileUtils.importExcel(file, InputExtProPlanModel.class,  new InputExtProPlanListener(inputExtProPlanModel, inputImportCostDao, new ImportCallBack() {
                @Override
                public void onSuccess() {
                    machining = "导入完成";
                }

                @Override
                public void onWarning(List list) {
                    machining = "部份数据无法导入，请点击下载检查数据内容";
                    exportErrorExcel(list);
                }
            }, inputImportCostDao.selectDictCommon()));
        } catch (IOException e) {
            return "导入失败";
        }
        return machining;
    }
    /**
     * 异常数据导出到Excel
     *
     * @param errorList
     */
    private void exportErrorExcel(List<InputExtProPlanModel> errorList) {
        if (errorList.size() <= 0) {
            return;
        }
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String path = uploadPath + user.getAccount() + "/";
        FileUtils.genExcel(path, "error.xls", InputExtProPlanModel.class, "未成功导入数据", errorList);
    }

    /**
     * 错误文件导出
     *
     * @param url 文件URL
     * @return 是否存在
     */
    public boolean exist(String url) {
        int i = url.lastIndexOf("files/");
        String path = uploadPath + url.substring(i + 6);
        File file = new File(path);
        if (!file.exists()) {
            return false;
        }
        return true;
    }

    /**
     * 外协计划清空逻辑验证
     * @param inputExtProPlanModel
     * @return InputExtProPlanModel
     */
    public int delExtPro(InputExtProPlanModel inputExtProPlanModel) {
        List<InputExtProPlanModel> inputExtProPlanModel1 = inputImportCostDao.selectExtPro1(inputExtProPlanModel);
        List<InputExtProPlanModel> inputExtProPlanModel2 = inputImportCostDao.selectExtPro2(inputExtProPlanModel);
        if (inputExtProPlanModel1.size() == 0 && inputExtProPlanModel2.size() > 0){
            return -1;
        }else if (inputExtProPlanModel1.size() == 0 && inputExtProPlanModel2.size() == 0){
            return 0;
        }else if(inputExtProPlanModel1.size() < inputExtProPlanModel2.size()){
            return -1;
        }else if(inputExtProPlanModel1.size() == inputExtProPlanModel2.size()){
            return 1;
        }else {
            return 0;
        }

    }
    /**
     * 清空对应外协计划明细
     * @param inputExtProPlanModel
     * @return InputExtProPlanModel
     */
    public int deleteExtPro(InputExtProPlanModel inputExtProPlanModel) {
        return inputImportCostDao.deleteExtPro(inputExtProPlanModel);
    }

    /**
     * 查询外协计划类型
     * @return list
     */
    public List<DictCommonModel> findTypes() {
        return inputImportCostDao.selectDictCommon();
    }


//    /**
//     * 查询分类列表
//     *
//     * @return list
//     */
//    public List<VwEquModel> findTypes() {
//        return inputImportCostDao.selectVwEqu();
//    }
}